## 项目的模型文件
# from FlaskOA import db
# from main import db
from FlaskOA import db


class Base(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    def save(self):
        db.session.add(self)
        db.session.commit()
    def update(self):
        db.session.commit()
    def delete(self):
        db.session.delete(self)
        db.session.commit()

### 职位和权限的关系表
pos_per = db.Table(
    "pos_per",    ## 表名
    # 职位表id
    db.Column("pos_id",db.Integer,db.ForeignKey("position.id")),
    ## 权限表的id
    db.Column("per_id",db.Integer,db.ForeignKey("permission.id"))
)

class Position(Base):
    """
    职位表
    """
    p_name = db.Column(db.String(32))       ## 职位名称
    p_level = db.Column(db.Integer)         ## 职位级别
    p_department = db.Column(db.Integer,db.ForeignKey("department.id"))
    p_person = db.relationship(
        "Person",   ## 类名
        backref = "our_position",    ###值可以随意定义
        uselist=False
    )
    p_permission = db.relationship(
        "Permission",   ## 类名
        secondary = pos_per,  ## 中间表 关系表
        backref = "p_position"
    )

class Person(Base):
    """
    用户表
    """
    # id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(32),unique=True)                                ## 登录名
    password = db.Column(db.String(32))                                ## 密码
    nickname = db.Column(db.String(32),default="")                                ## 姓名
    age = db.Column(db.Integer,default=18)                                        ## 年龄
    gender = db.Column(db.String(8),default="男")     ## 性别
    photo = db.Column(db.String(32),default="1.jpg")   ###头像
    email = db.Column(db.String(32),default="")        ## 邮箱
    address = db.Column(db.Text,default="")            ## 地址
    score = db.Column(db.Float,nullable=True)                                        ## 绩效
    p_position = db.Column(db.Integer,db.ForeignKey("position.id"))


class Permission(Base):
    per_name = db.Column(db.String(32))     ## 权限名字

class Department(Base):
    # 部门表
    d_name = db.Column(db.String(32))        ## 部门名称
    d_description = db.Column(db.Text)       ## 部门描述
    d_position = db.relationship(
        "Position",
        backref="p_depart"
    )

